import { Body, Controller, Delete, Get, Post, Put, Query } from "@nestjs/common";
import { UserService } from "./user.service";
import { ApiTags } from "@nestjs/swagger";
import { PaginationPipe } from "src/common/pipes/pagination.pipe";
import { ReqAddUserDto, ReqUpdateUserDto, ReqUserListDto } from "./dto/req-user.dto";
import { DataObj } from "src/common/class/data-obj.class";
import { ApiException } from "src/common/exceptions/api.exception";

@ApiTags("用户管理")
@Controller("system/user")
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get("getUserList")
  async getUserList(@Query(PaginationPipe) reqUserListDto: ReqUserListDto) {
    if (reqUserListDto.dept_id) {
      const res = await this.userService.getUserList(reqUserListDto);
      return DataObj.create(res);
    } else {
      throw new ApiException("部门不能为空！");
    }
  }

  // 新增用户
  @Post("addUser")
  async addUser(@Body() reqAddUserDto: ReqAddUserDto) {
    const user = await this.userService.findOneByUsername(reqAddUserDto.username);
    if (user) throw new ApiException("该用户已存在！");
    await this.userService.addUser(reqAddUserDto);
  }

  // 编辑用户
  @Post("updateUser")
  async updateUser(@Body() reqUpdateUserDto: ReqUpdateUserDto) {
    await this.userService.updateUser(reqUpdateUserDto);
  }

  //删除用户
  @Post("deleteUser")
  async deleteUser(@Body() userId: string) {
    await this.userService.deleteUser(userId);
  }
}
